package yunwo.cn.yzstation.model;

import android.app.ProgressDialog;
import android.util.Log;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.request.RequestCall;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.ISettingModel;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.api.okapi.LogoutCallBack;
import yunwo.cn.yzstation.api.okapi.UpDateNickCallBack;
import yunwo.cn.yzstation.api.okapi.UploadCallBack;
import yunwo.cn.yzstation.base.Yzapp;
import yunwo.cn.yzstation.bean.Bean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.view.activity.LoginActivity;
import yunwo.cn.yzstation.view.activity.MainActivity;
import yunwo.cn.yzstation.view.activity.SettingActivity;

/**
 * Created by lao on 2017/9/7.
 */

public class SettingActivityModel {
    SettingActivity mainActivity;
    ISettingModel imainModel;
    private ProgressDialog mMyDialog;
    public SettingActivityModel(SettingActivity mainActivity, ISettingModel imainModel) {
        this.mainActivity = mainActivity;
        this.imainModel = imainModel;
    }


    //上传头像
    public void postData(final File file, int capturePhoto) {
        OkHttpUtils.post().url(MyConstants.upload)
                .addHeader("Authorization",Yzapp.getInstance().getAuthorization())
                .addFile("file", file.getName(), file)
                .addParams("mobile", Yzapp.getInstance().getmobile())
                .build()
                .execute(new UploadCallBack(Bean.class,mainActivity,true){
                    @Override
                    public void myError(Call call, Exception e, int id) {
                        Log.d("zxcccccc", e.toString());
                    }

                    @Override
                    public void onResponse(Bean response, int id) {
                        if (response.getCode() == 200) {
                            Toast.makeText(mainActivity, "上传成功!", Toast.LENGTH_SHORT).show();

                            imainModel.UploadSuccess(response,file);
                        }else {
                            Toast.makeText(mainActivity,  response.getData(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });



    }
            //修改昵称
    public void upDateNick(final String nick) {
        Map<String, String> map = new HashMap<>();
        map.put("mobile", Yzapp.getInstance().getmobile());
        map.put("nickname", nick);
    HttpManager.hpptPost(MyConstants.nick_change,map).execute(new UpDateNickCallBack(Bean.class,mainActivity,true){
        @Override
        public void myError(Call call, Exception e, int id) {
            super.myError(call, e, id);
            imainModel.UpDateNickfailure();
        }

        @Override
        public void onResponse(Bean response, int id) {
            if (response.getCode() == 200) {
                imainModel.UpDateNickSuccess(nick);
            }
        }
    });

    }

    //退出登录
    public void Logout() {

        RequestCall build = OkHttpUtils.post().url(MyConstants.Logout)
                .addHeader("Authorization", Yzapp.getInstance().getAuthorization())
                .build();
        build.execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    imainModel.LogoutSuccess();
                }else {
                    Toast.makeText(mainActivity, msg, Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(mainActivity, "出错了~", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onBefore(Request request, int id) {
                if (mMyDialog == null) {
                    mMyDialog = ProgressDialog.show(mainActivity, null, "请稍等...");
                    mMyDialog.setCancelable(false);
                    mMyDialog.show();
                }
            }

            @Override
            public void onAfter(int id) {
                if (mMyDialog != null && mMyDialog.isShowing()) {
                    mMyDialog.dismiss();
                }
            }
        });
    }
}
/*new LogoutCallBack(Bean.class,mainActivity,true){
            @Override
            public void myError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(Bean response, int id) {

                if (response.getCode() == 200) {
                    imainModel.LogoutSuccess();
                }
            }
        }*/